package com.woniu.bh.config;

import com.woniu.bh.dto.DocDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @version: V1.0
 * @author: Xbao
 * @className: swagger配置类
 * @packageName: com.woniu.bh.backstage.config
 * @description:
 * @data: 2020-10-14 15:15
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(DocDto.class)
public class WebConfig {

    @Autowired
    private DocDto docDto;

    @Bean
    public Docket createDocket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        docket.apiInfo(new ApiInfoBuilder()
                .title(docDto.getTitle())
                .description(docDto.getDesc())
                .contact(new Contact(docDto.getAuthor().getName(),docDto.getAuthor().getUrl(),docDto.getAuthor().getEmail()))
                .version(docDto.getVersion())
                .build()
        )
                .globalOperationParameters(globalOperation())
                .select()
                .apis(RequestHandlerSelectors.basePackage(docDto.getBasePath()))
                .paths(PathSelectors.any())
                .build()
//                .globalOperationParameters(globalOperation())
                ;
        return docket;
    }

    private List<Parameter> globalOperation(){
        //添加head参数配置start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        //第一个token为传参的key，第二个token为swagger页面显示的值
        tokenPar.name("X-Token").description("token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }
}
